package com.fangyi.yunmusic.service.es;

import com.fangyi.yunmusic.pojo.es.AlbumCommentDocument;
import com.fangyi.yunmusic.pojo.es.PlaylistCommentDocument;
import com.fangyi.yunmusic.pojo.es.SongCommentDocument;
import com.fangyi.yunmusic.repository.es.AlbumCommentRepository;
import com.fangyi.yunmusic.repository.es.PlaylistCommentRepository;
import com.fangyi.yunmusic.repository.es.SongCommentRepository;
import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.WeightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;

@Service
public class CommentSearchService {

    @Autowired
    private SongCommentRepository songCommentRepository;
    @Autowired
    private PlaylistCommentRepository playlistCommentRepository;
    @Autowired
    private AlbumCommentRepository albumCommentRepository;

//    Page<SongCommentDocument>
    public Map<String ,Object> queryComment(Integer pageNum, Integer pageSize, String query) {

//        // 设置查询条件
//        FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
//                new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchPhraseQuery("content", query), new WeightBuilder().setWeight(100)) // 设置权重
//        };
//        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions)
//                .scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)     // 设置权重分求和模式
//                .setMinScore(10);   // 设置权重分最低分
//
//
//        Pageable pageable = PageRequest.of(pageNum, pageSize);// 设置分页

        NativeSearchQuery nativeSearchQuery =getNativeSearchQuery(pageNum,pageSize,query);
        Page<SongCommentDocument> songCommentDocuments=getSongCommentDocuments(nativeSearchQuery);
        Page<PlaylistCommentDocument> playlistCommentDocuments=getPlaylistCommentDocuments(nativeSearchQuery);
        Page<AlbumCommentDocument> albumCommentDocuments=getAlbumCommentDocuments(nativeSearchQuery);
        Map<String,Object> commentMap=new HashMap<>();
        commentMap.put("songCommentDocuments",songCommentDocuments);
        commentMap.put("playlistCommentDocuments",playlistCommentDocuments);
        commentMap.put("albumCommentDocuments",albumCommentDocuments);
        return commentMap;


    }
    public Page<SongCommentDocument> querySongComment(Integer pageNum, Integer pageSize, String query){
        return  getSongCommentDocuments(getNativeSearchQuery(pageNum,pageSize,query));
    }
    public Page<PlaylistCommentDocument> queryPlaylistComment(Integer pageNum, Integer pageSize, String query){
        return  getPlaylistCommentDocuments(getNativeSearchQuery(pageNum,pageSize,query));
    }
    public Page<AlbumCommentDocument> queryAlbumComment(Integer pageNum, Integer pageSize, String query){
        return  getAlbumCommentDocuments(getNativeSearchQuery(pageNum,pageSize,query));
    }
    private NativeSearchQuery getNativeSearchQuery(Integer pageNum, Integer pageSize, String query){
        // 设置查询条件
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchPhraseQuery("content", query), new WeightBuilder().setWeight(100)) // 设置权重
        };
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions)
                .scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)     // 设置权重分求和模式
                .setMinScore(10);   // 设置权重分最低分


        Pageable pageable = PageRequest.of(pageNum, pageSize);// 设置分页

        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(functionScoreQueryBuilder).build();
        return  nativeSearchQuery;

    }
    private Page<SongCommentDocument> getSongCommentDocuments(NativeSearchQuery nativeSearchQuery){
        return songCommentRepository.search(nativeSearchQuery);
    }
    private Page<PlaylistCommentDocument> getPlaylistCommentDocuments(NativeSearchQuery nativeSearchQuery){
        return playlistCommentRepository.search(nativeSearchQuery);
    }
    private Page<AlbumCommentDocument> getAlbumCommentDocuments(NativeSearchQuery nativeSearchQuery){
        return albumCommentRepository.search(nativeSearchQuery);
    }


//    public boolean isExistById(String id){
//        return songCommentRepository.existsById(id);
//    }


}
